//
//  YMTableView.m
//  BeautyList
//
//  Created by 李良林 on 2020/12/6.
//  Copyright © 2020 李良林. All rights reserved.
//

#import "YMTableView.h"
#import "Base64.h"
#import "MJEncryptStringData.h"
#import "LSTPopView.h"
#import "MJEncryptStringData.h"
#import "LWFuncView.h"
#import "ZGDeviceInfo.h"
#import "URLModel.h"
#import "LWccView.h"
#import "JQFMDB.h"

#define xioakfmdb [NSString stringWithFormat:@"xioakfmdb"]

// MainScreen Height&Width
#define Main_Screen_Height      [[UIScreen mainScreen] bounds].size.height

#define Main_Screen_Width       [[UIScreen mainScreen] bounds].size.width

//色值
#define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
#define RGB(r,g,b) RGBA(r,g,b,1.0f)

@implementation YMTableViewConfig

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.cellClass = [UITableViewCell class];
    }
    return self;
}
@end

@interface YMTableView()
@property (nonatomic,  strong) YMTableViewConfig *config;
@property (nonatomic,  assign) BOOL  hasLoadData;

@property (nonatomic, strong) LWFuncView *sideView;
@property (nonatomic, strong) LWccView *sideView1;

@property (nonatomic, strong) LSTPopView *popView;
@property (nonatomic, strong) LSTPopView *popView1;

@end

@implementation YMTableView

#pragma mark -------------------------------------视图-------------------------------------------

- (instancetype)initWithFrame:(CGRect)frame config:(YMTableViewConfig *)config
{
    self = [super initWithFrame:frame];
    if (self) {
        _config = config;
        _dataArray = @[].mutableCopy;
        self.userInteractionEnabled = YES;
        self.backgroundColor = [UIColor whiteColor];
        [self setupViews];
    }
    return self;
}

- (void)setupViews
{
    UIImageView *logo = [UIImageView new];
    
    NSString *menuButtonBase64 = LOGConfig;
    
    NSData* data = [[NSData alloc] initWithBase64EncodedString:menuButtonBase64 options:0];
    UIImage* menuButtonImage = [UIImage imageWithData:data];
    
    
    logo.image = menuButtonImage;
    
    logo.frame = CGRectMake(12, 0, 45, 45);
    
    UILabel *labtext = [UILabel new];
    labtext.textColor = RGB(128, 128, 128);
    labtext.text = @"我要开挂了";
    labtext.font = [UIFont boldSystemFontOfSize:14];
    labtext.frame = CGRectMake(65, 10, 200, 30);
    
    [self addSubview:logo];
    
    [self addSubview:labtext];
    
    
    [self addSubview:self.tableView];
    
    UIView *conview = [UIView new];
    
    conview.frame = CGRectMake(20, 210, 250, 135);
    conview.layer.cornerRadius = 6;
    conview.layer.masksToBounds = YES;
    
    conview.backgroundColor = RGBA(169, 169, 169, 0.9);
    [self addSubview:conview];
    
    UIButton *btn1 = [UIButton new];
    [btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn1.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    [btn1 setTitle:mj_OCString(_1105022233) forState:UIControlStateNormal];
    btn1.frame = CGRectMake(10, 2, 205, 30);
    [btn1 addTarget:self action:@selector(btn1cclick:) forControlEvents:UIControlEventTouchUpInside];
    [conview addSubview:btn1];
    
    UIView *line = [UIView new];
    
    line.backgroundColor = [UIColor  blackColor];
    line.frame = CGRectMake(5, 33, 210, 0.5);
    [conview addSubview:line];
    
    UIButton *btn2 = [UIButton new];
    btn2.frame = CGRectMake(10, 34, 210, 30);
    [btn2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn2.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    [btn2 addTarget:self action:@selector(btn2cclick:) forControlEvents:UIControlEventTouchUpInside];
    [btn2 setTitle:mj_OCString(_3867101821) forState:UIControlStateNormal];
    [conview addSubview:btn2];
    
    UIView *line1 = [UIView new];
    
    line1.backgroundColor = [UIColor  blackColor];
    line1.frame = CGRectMake(5, 65, 210, 0.5);
    [conview addSubview:line1];
    
    UIButton *btn3 = [UIButton new];
    btn3.frame = CGRectMake(10, 68, 210, 30);
    [btn3 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    btn3.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    [btn3 addTarget:self action:@selector(btn3cclick:) forControlEvents:UIControlEventTouchUpInside];
    [btn3 setTitle:mj_OCString(_1932560205) forState:UIControlStateNormal];
    [conview addSubview:btn3];
    
    UIView *line2 = [UIView new];
    line2.backgroundColor = [UIColor  blackColor];
    line2.frame = CGRectMake(5, 100, 210, 0.5);
    [conview addSubview:line2];
    
    UIButton *btn4 = [UIButton new];
    btn4.frame = CGRectMake(10, 104, 210, 30);
    [btn4 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    btn4.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    [btn4 addTarget:self action:@selector(btn4cclick:) forControlEvents:UIControlEventTouchUpInside];
    [btn4 setTitle:mj_OCString(_2076358540) forState:UIControlStateNormal];
    [conview addSubview:btn4];
    
}

#pragma mark -------------------------------------事件-------------------------------------------

#pragma mark - public

#pragma mark --- 首次加载数据，只加载一次
- (void)loadData{}
- (void)refreshData{}
- (void)beginRefresh:(BOOL)isPullDown{}
- (void)endRefresh:(BOOL)isPullDown count:(NSInteger)count{}

#pragma mark --- UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}

- (double)dataArraycount
{
    return 200;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *ID = NSStringFromClass(_config.cellClass);
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    cell.contentView.backgroundColor = [UIColor clearColor];
    cell.accessibilityIdentifier = _config.identity;
    if (_config.modelClass) {
        [cell setValue:_dataArray[indexPath.row] forKey:@"model"];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

#pragma mark -------------------------------------懒加载-----------------------------------------

- (UITableView *)tableView{
    if (!_tableView) {
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(20, 40, 250, 160) style:0];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.bounces = YES;
        tableView.tableFooterView = [[UIView alloc] init];
        
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        tableView.backgroundColor = [UIColor clearColor];
        tableView.layer.cornerRadius = 6;
        tableView.layer.masksToBounds = YES;
        _tableView = tableView;
        
        if (_config.rowHeight > 0) {
            _tableView.rowHeight = _config.rowHeight;
        }
        if (_config.sectionHeaderHeight > 0) {
            _tableView.sectionHeaderHeight = _config.sectionHeaderHeight;
        }
        if (_config.sectionFooterHeight > 0) {
            _tableView.sectionFooterHeight = _config.sectionFooterHeight;
        }
        
        NSString *ID = NSStringFromClass(_config.cellClass);
        if (_config.cellIsNib) {
            [tableView registerNib:[UINib nibWithNibName:ID bundle:nil] forCellReuseIdentifier:ID];
        }else{
            [tableView registerClass:_config.cellClass forCellReuseIdentifier:ID];
        }
    }
    return _tableView;
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */
///官方网站
- (void)btn1cclick:(UIButton *)btn {
    
    NSString *urlStr = [NSString stringWithFormat:@"%@", mj_OCString(_3894585273)];
    
    NSURL *URL = [NSURL URLWithString:urlStr];
    
    
    //        [[UIApplication sharedApplication]openURL:URL];
    
    [[UIApplication sharedApplication] openURL:URL options:@{} completionHandler:^(BOOL success) {
        
        
    }];
}

//点击加群
- (void)btn2cclick:(UIButton *)btn {
    
    NSString *urlStr = [NSString stringWithFormat:@"%@", mj_OCString(_4064956054)];
    
    NSURL *URL = [NSURL URLWithString:urlStr];
    
    [[UIApplication sharedApplication] openURL:URL options:@{} completionHandler:^(BOOL success) {
        
        
    }];
}

//拦截器1
- (void)btn4cclick:(UIButton *)btn {
    
    __weak typeof(self) weakSelf = self;
    
    CGFloat w;
    
    if (Main_Screen_Width > 667) {
        w = 660;
    } else {
        
        w = 355;
    }
    
    CGFloat h;
    
    if (Main_Screen_Height > 414) {
        
        h = 600;
        
    } else {
        
        h = 355;
    }
    
    if (_sideView1) {
        
        
    } else {
        
        self.sideView1 = [[LWccView alloc] initWithFrame:CGRectMake(0, 0, w,h)];
        self.sideView1.type = @"1";
        
        
        self.popView1 = [LSTPopView initWithCustomView:self.sideView1
                                            parentView:[UIApplication sharedApplication].keyWindow
                                              popStyle:LSTPopStyleSpringFromLeft
                                          dismissStyle:LSTDismissStyleSmoothToLeft];
        self.popView1.hemStyle = LSTHemStyleLeft;
        //        self.popView.groupId = mj_OCString(_1970084037);
        self.popView1.isStack = YES;
        if (Main_Screen_Width > 667) {
            
            self.popView1.adjustX = 44;
        }
        
        self.popView1.popDuration = 0.5;
        self.popView1.bgAlpha = 0.0;
        self.popView1.dismissDuration = 0.5;
        self.popView1.isClickFeedback = YES;
        self.popView1.bgClickBlock = ^{
            
           if ([ZGDeviceInfo currentDeviceInfo].installArrM.count > 0) {
                
                
                [[JQFMDB shareDatabase] jq_inDatabase:^{
                    
                    
                    [[JQFMDB shareDatabase] jq_deleteAllDataFromTable:xioakfmdb];
                    
                    [[JQFMDB shareDatabase] jq_insertTable:xioakfmdb dicOrModelArray:[ZGDeviceInfo currentDeviceInfo].installArrM.copy];
                    
                    
                    
                }];
                
            }
            
            
            [weakSelf.popView1 dismiss];
        };
        
    }
    
    if ([ZGDeviceInfo currentDeviceInfo].installArrM.count > 0) {
        
        
        [[JQFMDB shareDatabase] jq_inDatabase:^{
            
            
            [[JQFMDB shareDatabase] jq_deleteAllDataFromTable:xioakfmdb];
            
            [[JQFMDB shareDatabase] jq_insertTable:xioakfmdb dicOrModelArray:[ZGDeviceInfo currentDeviceInfo].installArrM.copy];
            
            
            
        }];
        
    }
    
    self.sideView1.titleArr = [ZGDeviceInfo currentDeviceInfo].installArrM.copy;
    
    [self.popView1 pop];
    
    
}

//拦截器1
- (void)btn3cclick:(UIButton *)btn {
    
    __weak typeof(self) weakSelf = self;
    
    CGFloat w;
    
    if (Main_Screen_Width > 667) {
        w = 660;
    } else {
        
        w = 355;
    }
    
    CGFloat h;
    
    if (Main_Screen_Height > 414) {
        
        h = 700;
        
    } else {
        
        h = 355;
    }
    
    if (_sideView) {
        
        
    } else {
        
        self.sideView = [[LWFuncView alloc] initWithFrame:CGRectMake(0, 0, w,h)];
        
        self.popView = [LSTPopView initWithCustomView:self.sideView
                                           parentView:[UIApplication sharedApplication].keyWindow
                                             popStyle:LSTPopStyleSpringFromLeft
                                         dismissStyle:LSTDismissStyleSmoothToLeft];
        self.popView.hemStyle = LSTHemStyleLeft;
        //        self.popView.groupId = mj_OCString(_1970084037);
        self.popView.isStack = YES;
        if (Main_Screen_Width > 667) {
            
            self.popView.adjustX = 44;
        }
        
        self.popView.popDuration = 0.5;
        self.popView.bgAlpha = 0.0;
        self.popView.dismissDuration = 0.5;
        self.popView.isClickFeedback = YES;
        self.popView.bgClickBlock = ^{
            
            [weakSelf.popView dismiss];
        };
        
    }
    
    self.sideView.titleArr = [ZGDeviceInfo currentDeviceInfo].selectedArrM.mutableCopy;
    
    [self.popView pop];
    
}

@end
